#pragma once
#include"RBTree.h"

namespace cyq
{
	template<class K,class V>
	class map
	{
	public:
		struct  KeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};

		typedef typename RBTree<K, pair<const K, V>, KeyOfT>::iterator iterator;
		typedef typename RBTree<K, pair<const K, V>, KeyOfT>::const_iterator const_iterator;


		iterator begin()
		{
			return _m.begin();
		}

		iterator end()
		{
			return _m.end();
		}

		const_iterator begin() const
		{
			return _m.begin();
		}

		const_iterator end() const
		{
			return _m.end();
		}

		//普通迭代器
		pair<iterator, bool>  Insert(const pair<K, V>& kv)
		{
			//树里面的普通迭代器
			pair<RBTree<K, pair<const K, V>, KeyOfT>::iterator, bool> ret = _m.Insert(kv);
			//用普通迭代器去拷贝构造普通迭代器
			return pair<iterator, bool>(ret.first, ret.second);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = Insert(make_pair(key, V()));
			return ret.first->second;
		}
	private:
		RBTree<K, pair<const K, V>, KeyOfT> _m;
	};
}
